import openpyxl,time
from openpyxl import load_workbook


class XLS():

    '''
    local_time    当前日期和时间
    request_raw   请求报文
    expected_result  预期结果
    actual_result 实际结果
    whether       是否通过

    file_path     读取得excel路径
    result_list   需要写入的信息
    read_list     读到的信息嵌套进list
    read_params   将每行的信息存入字典
    title_list    存储每列的标题
    read_params_list 将存入字典的信息，嵌套进列表

    def readXLS  读取文档
    def writeXLS 写入文档




    '''

    def __init__(self,file_path=''):

        self.local_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

        self.file_path = file_path

        self.read_params = {}

        self.read_list = []

        self.read_params_list = []


    def readXLS(self):

        read_param = load_workbook(self.file_path)

        table = read_param.active


        # 将读取到得excel存入list，再降list所有得值嵌套进另一个list
        for row in table.rows:

            self.list = [str(col.value) for col in row]

            self.read_list.append(self.list)


        title_list = self.read_list[0]

        self.read_list.pop(0)


        for i in range(len(self.read_list)):

            for j in range(len(title_list)):

                self.read_params[title_list[j]] = self.read_list[i][j]

            self.read_params_list.append(self.read_params.copy())

        print(self.read_params_list)

        return self.read_params_list,title_list



    def writeXLS(self,title_list=[],result_list=[]):

        self.title_list = title_list

        self.result_list = result_list

        self.title_list.append('请求接口')

        self.title_list.append('请求报文')

        self.title_list.append('是否通过')

        self.title_list.append('接口返回值')

        self.title_list_final = []

        self.title_list_final.append(self.title_list)


        report = openpyxl.Workbook()

        sheet = report.active

        sheet.title = "report"

        for title in self.title_list_final:

            sheet.append(title)


        for content in self.result_list:

            sheet.append(content)


        report.save('Review_report_'+str(self.local_time)+'.xlsx')

        report.close()
